import logging
from fastapi import APIRouter, Query

from app.controllers import category_controller
from app.schemas.base import Success, Fail
from app.schemas.category import CategoryCreate, CategoryUpdate

logger = logging.getLogger(__name__)
router = APIRouter()


@router.post("/create", summary="创建类别")
async def create_category(category_in: CategoryCreate):
    if await category_controller.is_exist(name=category_in.name):
        return Fail(message="类别名称已存在", status_code=400)
    await category_controller.create(obj_in=category_in)
    return Success(message="添加成功")


@router.post("/update", summary="更新类别")
async def update_category(category_in: CategoryUpdate):
    # 更新当前类别
    await category_controller.update(id=category_in.id, obj_in=category_in)
    return Success(message="修改成功")


@router.delete("/delete", summary="删除类别")
async def delete_category(category_id: int = Query(..., description="类别ID")):
    await category_controller.remove(id=category_id)
    return Success(message="删除成功")
